package com.tripspins.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tripspins.app.MapActivity;
import com.tripspins.app.R;
import com.tripspins.app.bo.Trip;
import com.tripspins.app.interfaces.OnFragmentInteractionListener;

import java.util.ArrayList;

/**
 * Created by joaortk on 01/08/13.
 */
public class TripListAdapter extends BaseAdapter {

    private final Context context;
    private final ArrayList<Trip> trips;
    private static LayoutInflater inflater;
    public TripListAdapter(Context context, ArrayList<Trip> trips)
    {
        this.trips = trips;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return trips.size();
    }

    @Override
    public Object getItem(int i) {
        return trips.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null)
        {
            view =  inflater.inflate(R.layout.trip_list_row, parent, false);
            ViewHolder vH = new ViewHolder();
            vH.txtTitle = (TextView)view.findViewById(R.id.trip_row_title);
            vH.txtPinCount = (TextView)view.findViewById(R.id.trip_row_pin_count);
            vH.txtAttCount = (TextView)view.findViewById(R.id.trip_row_att_count);
            vH.imgMap = (ImageView)view.findViewById(R.id.trip_row_map);
            view.setTag(vH);
        }
        ViewHolder holder = (ViewHolder)view.getTag();
        holder.txtTitle.setText(trips.get(position).getDescription());
        holder.txtPinCount.setText(String.valueOf(trips.get(position).getPins().size()));
        holder.txtAttCount.setText(String.valueOf(trips.get(position).getCountAttachments()));
        holder.position = position;
        holder.imgMap.setImageURI(trips.get(position).getRandomPicture());
        //holder.imgMap.setHorizontalFadingEdgeEnabled(true);


      /*  try {
            new AsyncTask<ViewHolder, Void, Void>() {
                private ViewHolder viewHolder;

                @Override
                protected Void doInBackground(ViewHolder... params) {
                    viewHolder = params[0];
                    try {
                        viewHolder.imgMap.setImageURI(trips.get(position).getRandomPicture());
                        viewHolder.imgMap.setHorizontalFadingEdgeEnabled(true);
                    } catch (Throwable e) {
                        try {
                            System.gc();
                            viewHolder.imgMap.setImageURI(trips.get(position).getRandomPicture());
                            viewHolder.imgMap.setHorizontalFadingEdgeEnabled(true);
                        } catch (Throwable e2) {

                        }
                    }
                    return null;

                }
            }.execute(holder);
        }catch(Exception e){
            Log.e("TP", e.getMessage());}
*/

        final int pos = position;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    ((OnFragmentInteractionListener)context).viewTrip(trips.get(pos).getId());
            }
        });
        return view;
    }

    class ViewHolder
    {
        public TextView txtTitle;
        public TextView txtPinCount;
        public TextView txtAttCount;
        public ImageView imgMap;
        public int position;

    }
}
